# Standard FIND_PACKAGE module for mcsapi, sets the following variables:
#   - MCSAPI_FOUND
#   - MCSAPI_INCLUDE_DIRS (only if MCSAPI_FOUND)
#   - MCSAPI_LIBRARIES (only if MCSAPI_FOUND)
#   - MCSAPI_RUNTIME_LIBRARIES (only if MCSAPI_FOUND)
#   - JAVA_MCSAPI_LIBRARIES (only if MCSAPI_FOUND)
#   - JAVA_MCSAPI_RUNTIME_LIBRARIES (only if MCSAPI_FOUND)

SET(FIND_LIBRARY_USE_LIB64_PATHS ON)

# Store the old library prefixes and suffixes
SET(CMAKE_FIND_LIBRARY_PREFIXES_BAK "${CMAKE_FIND_LIBRARY_PREFIXES}")
SET(CMAKE_FIND_LIBRARY_SUFFIXES_BAK "${CMAKE_FIND_LIBRARY_SUFFIXES}")

# Try to find the header
FIND_PATH(MCSAPI_INCLUDE_DIR NAMES mcsapi.h PATH_SUFFIXES libmcsapi include/libmcsapi HINTS $ENV{MCSAPI_INSTALL_DIR})

# Try to find the mcsapi library
FIND_LIBRARY(MCSAPI_LIBRARY NAMES mcsapi HINTS $ENV{MCSAPI_INSTALL_DIR} PATH_SUFFIXES lib)

# Try to find the mcsapi runtime library and depenent libraries
SET(CMAKE_FIND_LIBRARY_PREFIXES "")
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".dll" ".DLL")

find_library(
    MCSAPI_LIBXML2_RUNTIME_LIBRARY
    NAMES LIBXML2 libxml2
    HINTS $ENV{MCSAPI_INSTALL_DIR}
    PATH_SUFFIXES lib
)

find_library(
    MCSAPI_LIBICONV_RUNTIME_LIBRARY
    NAMES LIBICONV libiconv
    HINTS $ENV{MCSAPI_INSTALL_DIR}
    PATH_SUFFIXES lib
)
    
find_library(
    MCSAPI_LIBUV_RUNTIME_LIBRARY
    NAMES LIBUV libuv
    HINTS $ENV{MCSAPI_INSTALL_DIR}
    PATH_SUFFIXES lib
)

find_library(
    MCSAPI_RUNTIME_LIBRARY
    NAMES MCSAPI mcsapi
    HINTS $ENV{MCSAPI_INSTALL_DIR}
    PATH_SUFFIXES lib
)

# Try to find the javamcsapi runtime libraries
find_library(
    JAVA_MCSAPI_RUNTIME_LIBRARY
    NAMES JAVAMCSAPI javamcsapi
    HINTS $ENV{MCSAPI_INSTALL_DIR}
    PATH_SUFFIXES lib
)

# Try to find the javamcsapi library [.jar]
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".jar" ".JAR")
find_library(
    JAVA_MCSAPI_LIBRARY
    NAMES JAVAMCSAPI-${VERSION} javamcsapi-${VERSION}
    HINTS $ENV{MCSAPI_INSTALL_DIR}
    PATH_SUFFIXES lib/java
)

# Handle the QUIETLY/REQUIRED arguments, set MCSAPI_FOUND if all variables are
# found
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(MCSAPI
                                  REQUIRED_VARS
                                  MCSAPI_LIBRARY
                                  MCSAPI_INCLUDE_DIR
								  MCSAPI_LIBXML2_RUNTIME_LIBRARY
								  MCSAPI_LIBICONV_RUNTIME_LIBRARY
								  MCSAPI_LIBUV_RUNTIME_LIBRARY
								  MCSAPI_RUNTIME_LIBRARY
								  JAVA_MCSAPI_RUNTIME_LIBRARY
								  JAVA_MCSAPI_LIBRARY)

# Hide internal variables
MARK_AS_ADVANCED(MCSAPI_INCLUDE_DIR MCSAPI_LIBRARY MCSAPI_LIBXML2_RUNTIME_LIBRARY MCSAPI_LIBICONV_RUNTIME_LIBRARY MCSAPI_LIBUV_RUNTIME_LIBRARY MCSAPI_RUNTIME_LIBRARY JAVA_MCSAPI_RUNTIME_LIBRARY JAVA_MCSAPI_LIBRARY)

# Set standard variables
IF(MCSAPI_FOUND)
    SET(MCSAPI_INCLUDE_DIRS "${MCSAPI_INCLUDE_DIR}")
    SET(MCSAPI_LIBRARIES "${MCSAPI_LIBRARY}")
    SET(MCSAPI_RUNTIME_LIBRARIES "${MCSAPI_RUNTIME_LIBRARY}" "${MCSAPI_LIBXML2_RUNTIME_LIBRARY}" "${MCSAPI_LIBICONV_RUNTIME_LIBRARY}" "${MCSAPI_LIBUV_RUNTIME_LIBRARY}")
    SET(JAVA_MCSAPI_LIBRARIES "${JAVA_MCSAPI_LIBRARY}")
    SET(JAVA_MCSAPI_RUNTIME_LIBRARIES "${JAVA_MCSAPI_RUNTIME_LIBRARY}")
ENDIF()

# Restore the old library prefixes and suffixes
SET(CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES_BAK}")
SET(CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES_BAK}")